package config;

import interceptor.FirstInterceptor;
import interceptor.FourthInterceptor;
import interceptor.SecondInterceptor;
import interceptor.ThirdInterceptor;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.*;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

import javax.annotation.Resource;

@Configuration
@EnableWebMvc  //开启SpringMVC
@ComponentScan({"controller", "interceptor"})
public class SpringMVCConfig implements WebMvcConfigurer {
    // 视图解析器
    public void configureViewResolvers(ViewResolverRegistry registry) {
        InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
        // 就会跟controller方法的返回结果一起组装成页面路径
        viewResolver.setSuffix(".html");
        viewResolver.setPrefix("/");
        viewResolver.setExposeContextBeansAsAttributes(true);
        registry.viewResolver(viewResolver);
    }

    // 允许静态资源访问
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }

    // 注册拦截器
    @Resource
    private FirstInterceptor firstInterceptor;
    @Resource
    private SecondInterceptor secondInterceptor;
    @Resource
    private ThirdInterceptor thirdInterceptor;
    @Resource
    private FourthInterceptor fourthInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 注册拦截器
        registry.addInterceptor(firstInterceptor)
                // 对哪些请求拦截
                .addPathPatterns("/**")
                // 对哪些请求排除(不拦截)
                .excludePathPatterns("/first/a").order(4);
        registry.addInterceptor(fourthInterceptor).addPathPatterns("/**").order(1);
        registry.addInterceptor(secondInterceptor).addPathPatterns("/**").order(2);
        registry.addInterceptor(thirdInterceptor).addPathPatterns("/**").order(3);
    }
}